package com.ldn.server.mqtt.handler;

import com.ldn.server.mqtt.covert.MqttCovert;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.dromara.mica.mqtt.core.server.auth.IMqttServerPublishPermission;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.tio.core.ChannelContext;

/**
 * 发布消息权限验证器
 *
 * @author 李大牛家的牛 (scafel@163.com)
 * @product serverless
 * @className PublishPermissionHandler
 * @createDate 2025/3/4 - 13:13
 * @description 发布消息权限验证器
 **/
@Slf4j
@Configuration(proxyBeanMethods = false)
public class PublishPermissionHandler implements IMqttServerPublishPermission {

    @Resource
    private ApplicationContext applicationContext;

    @Override
    public boolean hasPermission(ChannelContext context, String clientId, String topic, MqttQoS qoS, boolean isRetain) {
        log.info("发布消息权限验证器: context => {}", context);
        log.info("发布消息权限验证器: clientId:{},topic:{},qoS:{},isRetain:{}", clientId, topic, qoS, isRetain);
        MqttCovert covert = applicationContext.getBean(MqttCovert.class);
        return covert.checkCanPublish(clientId, topic, qoS.value(), isRetain);
    }
}
